/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visao;

import Controle.Controlador;
import Modelo.Endereco;
import Visao.Funcionario.Pedido.EditarPedido;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;



/**
 *
 * @author Andre Luiz
 */
public class EnderecoEntrega extends javax.swing.JFrame {
    boolean update = false;
    Integer codigoClienteUpdate;
    Integer codigoPedido;
    Integer codigoEndEntrega;
    NovoPedido telaPedido;
    EditarPedido telaEditarPedido;
    /**
     * Creates new form EnderecoEntrega
     */
    public EnderecoEntrega() {
        initComponents();
    }
    
    public EnderecoEntrega(NovoPedido telaPedido) {
        initComponents();
        this.telaPedido = telaPedido;
        if(Controlador.getPedidoAtual().getEndEntrega()!= null){
            setDadosVisao(Controlador.getPedidoAtual().getEndEntrega());
        }
    }
    
    public EnderecoEntrega(EditarPedido telaEditarPedido, int codigoPedido) {
        initComponents();
        this.telaEditarPedido = telaEditarPedido;
        this.codigoPedido = codigoPedido;
        this.codigoEndEntrega = Controlador.get_CodigoEnderecoEntregaPedido(codigoPedido);
        if(this.codigoEndEntrega!=null){
            Endereco end= Controlador.carregarDadosBD_EnderecoEntrega(codigoEndEntrega);
            if(end!=null){
                setDadosVisao(end);
            }
        }
    }
    
    public Endereco getDadosVisao(){
        Endereco end= new Endereco();
        end.setBairro(jcbBairro.getSelectedItem().toString());
        end.setRua(""+jftRua.getText().toUpperCase());
        end.setNum(""+jtfNum.getText());
        end.setComplemento(""+jtfComp.getText());
        
        return end;
    }
    
    private void validarEntradaDados() throws Exception {
        //jcbCategoria jtaIngredientes jtfSabor
        Matcher letras ;
        Matcher numeros;
        Matcher espaco;

        
        if (jcbBairro.getSelectedIndex() == 0) {
            throw new Exception("Informe o Bairro do Cliente.");
        }
        letras = Pattern.compile("[a-zA-Z]").matcher(jftRua.getText());
        if (!letras.find()) {
            throw new Exception("Informe a rua do Cliente.");
        }
        numeros = Pattern.compile("[0-9]").matcher(jtfNum.getText());
        if(!numeros.find()) {
            throw new Exception("Informe o numero do Endereço do Cliente.");
        }
    }
    
    private void setDadosVisao(Endereco end) {
        try {
            //System.out.println(param.toString());
            jcbBairro.setSelectedItem(end.getBairro());
            jftRua.setText(end.getRua());
            jtfNum.setText(end.getNum().replace(" ", ""));
            jtfComp.setText(end.getComplemento());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Setar dados do Cliente na tela", "Erro!", 0);
        }

    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbBairro = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jftRua = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfNum = new javax.swing.JFormattedTextField();
        jtfComp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jCancelarB = new javax.swing.JButton();
        jConcluirB = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Endereço de Entrega");

        jcbBairro.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " ", "Alto Alegre", "Brasília", "Brazmadeira", "Canadá", "Cancelli", "Cascavel Velho", "Cataratas", "Centro", "Coqueiral", "Country", "Esmeralda", "Floresta", "Guarujá", "Interlagos", "Maria Luiza", "Morumbi", "Neva", "Pacaembu", "Parque São Paulo", "Parque Verde", "Periolo", "Pioneiros Catarinenses", "Recanto Tropical", "Região do Lago", "Santa Cruz", "Santa Felicidade", "Santo Onofre", "Santos Dumont", "São Cristóvão", "Universitário", "XIV De Novembro" }));
        jcbBairro.setPreferredSize(new java.awt.Dimension(196, 20));
        jcbBairro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbBairroActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Bairro:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Rua:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Nº:");

        try {
            jtfNum.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfNum.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);
        jtfNum.setPreferredSize(new java.awt.Dimension(75, 20));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Comp:");

        jCancelarB.setText("Cancelar");
        jCancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelarBActionPerformed(evt);
            }
        });

        jConcluirB.setText("OK");
        jConcluirB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jConcluirBActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel1.setText("Endereço de Entrega");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jCancelarB, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5)
                                .addComponent(jConcluirB, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jcbBairro, 0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jftRua, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel4)))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtfNum, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jtfComp, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jcbBairro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jftRua)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jtfNum, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfComp)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCancelarB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jConcluirB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-342)/2, (screenSize.height-244)/2, 342, 244);
    }// </editor-fold>//GEN-END:initComponents

    private void jcbBairroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbBairroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbBairroActionPerformed

    private void jConcluirBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jConcluirBActionPerformed
        // Botão Conluir
        try {
            if(telaPedido!=null){
                validarEntradaDados();
                Controlador.getPedidoAtual().setEndEntrega(getDadosVisao());
                this.setVisible(false);
                JOptionPane.showMessageDialog(rootPane, "Endereço de Entrega adicionado com sucesso!", "Ok", 1);
            }else{
                validarEntradaDados();
                if(this.codigoEndEntrega==null){
                    Controlador.salvar_EnderecoEntregaPedido(getDadosVisao(), this.codigoPedido);  
                }else{
                    Controlador.editar_EnderecoEntregaPedido(getDadosVisao(), this.codigoPedido, this.codigoEndEntrega);  
                }
                this.setVisible(false);
                JOptionPane.showMessageDialog(rootPane, "Endereço de Entrega adicionado com sucesso!", "Ok", 1);
            }
                
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Adicionar o Endereço de Entrega!\n"+e.getMessage(), "Erro!", 0);
        }
        
    }//GEN-LAST:event_jConcluirBActionPerformed

    private void jCancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelarBActionPerformed
        this.setVisible(false); 
    }//GEN-LAST:event_jCancelarBActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancelarB;
    private javax.swing.JButton jConcluirB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JComboBox jcbBairro;
    private javax.swing.JTextField jftRua;
    private javax.swing.JTextField jtfComp;
    private javax.swing.JFormattedTextField jtfNum;
    // End of variables declaration//GEN-END:variables
}
